package controllers

import "LoginTemplate/models"

type ConfigurationFilesController struct {
	BaseController
}

type ConfigurationFiles struct {
	ID    int    `json:"id"`
	Name  string `json:"name"`
	Value string `json:"value"`
}

func (u *ConfigurationFilesController) InsertConfigurationFiles() {
	var client ConfigurationFiles
	err := u.ShouldBind(&client)
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	sql := "insert into configuration_files ( name,value ) values (?,?)"
	tx := models.DB.Debug().Exec(sql, client.Name, client.Value)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

func (u *ConfigurationFilesController) UpdateConfigurationFiles() {
	var client ConfigurationFiles
	err := u.ShouldBind(&client)
	if err != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	sql := "update configuration_files set `name` = ? ,`value` = ? where id = ?"
	tx := models.DB.Debug().Exec(sql, client.Name, client.Value, client.ID)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

type ConfigurationFilesData struct {
	Code        int                  `json:"code"`
	Msg         string               `json:"msg"`
	Data        []ConfigurationFiles `json:"data"`
	Sum         int                  `json:"sum"`         //总页数
	CurrentPage int                  `json:"currentPage"` //当前页
	TotalNumber int                  `json:"totalNumber"` //总条数
}

// 查询分类
func (u *ConfigurationFilesController) SelectConfigurationFiles() {
	var pictureList PictureList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var client []ConfigurationFiles
	sql := "select * from configuration_files limit ? , ?"
	tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&client)
	sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM configuration_files"
	models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
	sqlNumBer := "select count(*) from configuration_files"
	models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
	if tx.Error != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var picturedata ConfigurationFilesData
	picturedata.Code = 200
	picturedata.Data = client
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	//return picturedata
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}
